/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package weka;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import weka.attributeSelection.CfsSubsetEval;
import weka.attributeSelection.GreedyStepwise;
import weka.classifiers.Evaluation;
import weka.classifiers.meta.AttributeSelectedClassifier;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

/**
 *
 * @author JeanW
 */
public class ChargerArffAction extends AbstractAction{
    private MainWindow fenetre;

    public ChargerArffAction(MainWindow fenetre ,String texte) {
        super(texte);
        this.fenetre = fenetre;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MonFiltre arff = new MonFiltre( 
           new String[]{"arff"},
           "Fichiers ARFF (*.arff)");
        JFileChooser choix = new JFileChooser();
        choix.addChoosableFileFilter(arff);
        int retour=choix.showOpenDialog(fenetre);
        if(retour==JFileChooser.APPROVE_OPTION){
           // un fichier a été choisi (sortie par OK)
           // nom du fichier  choisi 
           fenetre.getTextField().setText(choix.getSelectedFile().getName());
           // chemin absolu du fichier choisi
           fenetre.setFile(choix.getSelectedFile().getAbsolutePath());
          // fenetre.getEtude().setArffPath(choix.getSelectedFile().getAbsolutePath());
        }
           fenetre.getTextArea().setText("");
           String texte = "";
           //fenetre.getTextArea().setText(texte);
        DataSource source = null;
            try {
                source = new DataSource(fenetre.getFile());
            } catch (Exception ex) {
                Logger.getLogger(ChargerArffAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        Instances data = null;
            try {
                data = source.getDataSet();
            } catch (Exception ex) {
                Logger.getLogger(ChargerArffAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        if (data.classIndex() == -1)
          data.setClassIndex(data.numAttributes() - 1);
        
        fenetre.getTextArea().setText(texte);
        AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
        CfsSubsetEval eval = new CfsSubsetEval();
        GreedyStepwise search = new GreedyStepwise();
        search.setSearchBackwards(true);
        J48 base = new J48();
        try {
            base.buildClassifier(data);
        } catch (Exception ex) {
            Logger.getLogger(ChargerArffAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        classifier.setClassifier(base);
        classifier.setEvaluator(eval);
        classifier.setSearch(search);

        Evaluation evaluation = null;
            try {
                evaluation = new Evaluation(data);
            } catch (Exception ex) {
                Logger.getLogger(ChargerArffAction.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                evaluation.crossValidateModel(classifier, data, 10, new Random(1));
            } catch (Exception ex) {
                Logger.getLogger(ChargerArffAction.class.getName()).log(Level.SEVERE, null, ex);
            }
                texte = texte + evaluation.toSummaryString()+ "\n";
                fenetre.getTextArea().setText(texte);
                fenetre.setTree(classifier.toString());
                
        FileWriter fw = null;
        File file = new File("temp.txt");
        try {
            fw = new FileWriter(file);
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fw.write(fenetre.getTree());
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            fw.close();
        } catch (IOException ex) {
            Logger.getLogger(exportXMLAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        fenetre.getBoutonVisu().setEnabled(true);
        //fenetre.getExportXML().setEnabled(true);  
        fenetre.getBoutonInsert().setEnabled(true);
    }
    
}
